package com.xinghai.system.service.impl;

import java.util.Collections;
import java.util.List;
import com.xinghai.common.utils.DateUtils;
import com.xinghai.common.utils.StringUtils;
import com.xinghai.system.domain.dto.LoadFeeDetailDTO;
import com.xinghai.system.domain.vo.XhStaticsReportQueryVo;
import com.xinghai.system.util.XHUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Service;
import com.xinghai.system.mapper.XhVehicleOrderFeeMapper;
import com.xinghai.system.domain.XhVehicleOrderFee;
import com.xinghai.system.service.IXhVehicleOrderFeeService;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 订单费用Service业务层处理
 * 
 * @author smy
 * @date 2024-08-02
 */
@Service
public class XhVehicleOrderFeeServiceImpl extends ServiceImpl<XhVehicleOrderFeeMapper, XhVehicleOrderFee> implements IXhVehicleOrderFeeService
{
    @Autowired
    private XhVehicleOrderFeeMapper xhVehicleOrderFeeMapper;

    @Autowired
    private SqlSessionFactory sqlSessionFactory;
    /**
     * 查询订单费用
     * 
     * @param id 订单费用主键
     * @return 订单费用
     */
    @Override
    public XhVehicleOrderFee getInfo(Long id)
    {
        return xhVehicleOrderFeeMapper.getInfo(id);
    }

    /**
     * 查询订单费用列表
     * 
     * @param xhVehicleOrderFee 订单费用
     * @return 订单费用
     */
    @Override
    public List<XhVehicleOrderFee> list(XhVehicleOrderFee xhVehicleOrderFee)
    {
            xhVehicleOrderFee.setDelFlag("0");
        return xhVehicleOrderFeeMapper.list(xhVehicleOrderFee);
    }

    /**
     * 新增订单费用
     * 
     * @param xhVehicleOrderFee 订单费用
     * @return 结果
     */
    @Override
    public int insert(XhVehicleOrderFee xhVehicleOrderFee)
    {
        xhVehicleOrderFee.setCreateTime(DateUtils.getNowDate());
        return xhVehicleOrderFeeMapper.insert(xhVehicleOrderFee);
    }
    /**
     * 批量新增订单费用
     *
     * @param xhVehicleOrderFeeList 订单费用
     * @return 结果
     */
    @Override
    public int insertBatch(List<XhVehicleOrderFee> xhVehicleOrderFeeList)
    {
        if (xhVehicleOrderFeeList.isEmpty()){
            return 0;
        }
        return xhVehicleOrderFeeMapper.insertBatch(xhVehicleOrderFeeList);
    }
    /**
     * 修改订单费用
     * 
     * @param xhVehicleOrderFee 订单费用
     * @return 结果
     */
    @Override
    public int update(XhVehicleOrderFee xhVehicleOrderFee)
    {
        xhVehicleOrderFee.setUpdateTime(DateUtils.getNowDate());
        return xhVehicleOrderFeeMapper.update(xhVehicleOrderFee);
    }

    /**
     * 批量删除订单费用
     * 
     * @param ids 需要删除的订单费用主键
     * @return 结果
     */
    @Override
    public int deleteBatch(Long[] ids)
    {
        return xhVehicleOrderFeeMapper.deleteBatch(ids);
    }

    /**
     * 删除订单费用信息
     * 
     * @param id 订单费用主键
     * @return 结果
     */
    @Override
    public int delete(Long id)
    {
        return xhVehicleOrderFeeMapper.delete(id);
    }

    /**
     * 导入订单费用信息
     *
     * @param list 订单费用信息集合
     * @return 结果
     */
    @Override
    @Transactional
    public boolean importData(List<XhVehicleOrderFee> list){
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        try {
            XhVehicleOrderFeeMapper xhVehicleOrderFeeMapper = sqlSession.getMapper(XhVehicleOrderFeeMapper.class);
            list.stream().forEach(item -> xhVehicleOrderFeeMapper.insert(item));
            // 提交数据
            sqlSession.commit();
            sqlSession.rollback();
        } catch (Exception e) {
            sqlSession.rollback();
            return false;
        } finally {
            sqlSession.close();
        }
        return true;
    }

    @Override
    public List<LoadFeeDetailDTO> loadFeeDetailPage(XhStaticsReportQueryVo xhStaticsReportQueryVo) {
        List<LoadFeeDetailDTO> detailDTOList = xhVehicleOrderFeeMapper.loadFeeDetailPage(xhStaticsReportQueryVo);
        detailDTOList.forEach(item -> {
            //[{北京市,北京市,东城区=1111}, {河北省,唐山市,路北区=111}]
            if (StringUtils.isNotEmpty(item.getBeginAddr())) {
                item.setBeginAddr(XHUtils.parseBeginAddr(item.getBeginAddr()));
            }
        });
        return detailDTOList;
    }
}
